package com.guldan.jaina.manager;

import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.householdareainfo.HouseholdAreaInfoDTO;
import com.guldan.jaina.utils.HouseholdDomainConvertUtils;
import com.guldan.mapper.db.entity.HouseholdAreaInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.guldan.core.manager.BaseManager;
import com.guldan.core.search.Pagination;
import com.guldan.mapper.db.entity.HouseholdAreaInfoExample;
import com.guldan.mapper.db.mapper.HouseholdAreaInfoMapperExt;

import java.util.List;

/**
 * Created by Silas.
 * Date: 2016/11/7
 * Time: 22:55
 */
@Service
public class HouseholdAreaInfoManager extends BaseManager {

    @Autowired
    private HouseholdAreaInfoMapperExt householdAreaInfoMapperExt;

    //获取单个家庭土地信息，通过householdAreaInfoId
    public HouseholdAreaInfoDTO get(Integer householdAreaInfoId) {
        return HouseholdDomainConvertUtils.convert(householdAreaInfoMapperExt.selectByPrimaryKey(householdAreaInfoId));
    }

    //获取单个家庭土地信息，通过householdId
    public HouseholdAreaInfoDTO getByHouseholdId(Integer householdId) {
        HouseholdAreaInfoExample example = new HouseholdAreaInfoExample();
        HouseholdAreaInfoExample.Criteria criteria = example.createCriteria();
        criteria.andHouseholdIdEqualTo(householdId);
        criteria.andIsDeleteEqualTo(false);
        List<HouseholdAreaInfo> householdAreaInfoList = householdAreaInfoMapperExt.selectByExample(example);
        HouseholdAreaInfoDTO dto = new HouseholdAreaInfoDTO();
        if (householdAreaInfoList != null && householdAreaInfoList.size() > 0) {
            dto = HouseholdDomainConvertUtils.convert(householdAreaInfoList.get(0));
        }
        return dto;
    }

    //新增/更新单个家庭土地信息，传入：HouseholdAreaInfoDTO
    public HouseholdAreaInfoDTO save(HouseholdAreaInfoDTO dto) {

        HouseholdAreaInfo entity;
        if (dto.getHouseholdAreaInfoId() != null && dto.getHouseholdAreaInfoId() > 0) {
            entity = householdAreaInfoMapperExt.selectByPrimaryKey(dto.getHouseholdAreaInfoId());

            entity.setHouseholdId(dto.getHouseholdId());
            entity.setFarmToForestNumber(dto.getFarmToForestNumber());
            entity.setFarmLandArea(dto.getFarmLandArea());
            entity.setFineBreedArea(dto.getFineBreedArea());
            entity.setEconomyArea(dto.getEconomyArea());
            entity.setAgriculturalArea(dto.getAgriculturalArea());
            entity.setFillGrassArea(dto.getFillGrassArea());
            entity.setForestEcologyArea(dto.getForestEcologyArea());
            entity.setPlainsEcologyArea(dto.getPlainsEcologyArea());
            entity.setConsolidateReturnArea(dto.getConsolidateReturnArea());
            entity.setPlainsGrazingArea(dto.getPlainsGrazingArea());
            entity.setReturnArea(dto.getReturnArea());
            entity.setReservoirsMove(dto.getReservoirsMove());

            entity.setModifyTime(DateUtil.getCurrentTime());
            //entity.setModifier(dto.getModifier());
            householdAreaInfoMapperExt.updateByPrimaryKeySelective(entity);
        } else {
            entity = new HouseholdAreaInfo();

            entity.setHouseholdId(dto.getHouseholdId());
            entity.setFarmToForestNumber(dto.getFarmToForestNumber());
            entity.setFarmLandArea(dto.getFarmLandArea());
            entity.setFineBreedArea(dto.getFineBreedArea());
            entity.setEconomyArea(dto.getEconomyArea());
            entity.setAgriculturalArea(dto.getAgriculturalArea());
            entity.setFillGrassArea(dto.getFillGrassArea());
            entity.setForestEcologyArea(dto.getForestEcologyArea());
            entity.setPlainsEcologyArea(dto.getPlainsEcologyArea());
            entity.setConsolidateReturnArea(dto.getConsolidateReturnArea());
            entity.setPlainsGrazingArea(dto.getPlainsGrazingArea());
            entity.setReturnArea(dto.getReturnArea());
            entity.setReservoirsMove(dto.getReservoirsMove());

            //entity.setCreator(dto.getModifier());
            entity.setCreateTime(DateUtil.getCurrentTime());
            householdAreaInfoMapperExt.insertSelective(entity);
        }
        dto = HouseholdDomainConvertUtils.convert(entity);
        return dto;
    }

    //删除单个家庭土地信息
    public Boolean delete(Integer householdLandInfoId) {
        return null;
    	/*HouseholdLandInfo entity = householdLandInfoMapperExt.selectByPrimaryKey(householdLandInfoId);
        entity.setIsDelete(true);
        return householdLandInfoMapperExt.updateByPrimaryKeySelective(entity) > 0;*/
    }

    /*//分页查询家庭土地信息列表
    public Pagination<HouseholdAreaInfoDTO> list(HouseholdLandInfoSearchDTO householdLandInfoSearchDTO) {
        return null;
        *//*Pagination<HouseholdAreaInfoDTO> result = new Pagination<HouseholdAreaInfoDTO>(householdLandInfoSearchDTO.getCurrentPage(), householdLandInfoSearchDTO.getPerPage());
        HouseholdLandInfoExample example = buildSearchExample(householdLandInfoSearchDTO);
        int total = householdLandInfoMapperExt.countByExample(example);
        result.setTotalCount(total);
        if (total > 0) {
            List<HouseholdAreaInfoDTO> data = new ArrayList<HouseholdAreaInfoDTO>();
            List<HouseholdLandInfo> list = householdLandInfoMapperExt.selectByExampleWithPaging(example, result.getOffset(), result.getPageSize());
            for (HouseholdLandInfo entity : list) {
            	HouseholdAreaInfoDTO dto = HouseholdLandInfoDomainConvertUtils.convert(entity);
                data.add(dto);
            }
            result.setList(data);
        }
        return result;*//*
    }*/

    /*//build查询Example
    private HouseholdAreaInfoExample buildSearchExample(HouseholdA search) {
    	HouseholdLandInfoExample example = new HouseholdLandInfoExample();
    	HouseholdLandInfoExample.Criteria criteria = example.createCriteria();

        if (search.getHouseholdLandInfoId() != null && search.getHouseholdLandInfoId() > 0) {
            criteria.andHouseholdLandInfoIdEqualTo(search.getHouseholdLandInfoId());
        }
        criteria.andIsDeleteEqualTo(Boolean.FALSE);
        if (StringUtils.isNotBlank(search.getOrderByClause())) {
            example.setOrderByClause(search.getOrderByClause());
        } else {
            example.setOrderByClause("household_land_info_id asc");
        }
        return example;
        return null;
    }*/

}
